package com.example.android.ui.mine;

import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.android.R;
import com.example.android.bean.BaseBean;
import com.example.android.bean.Goods;
import com.example.android.bean.Mine;
import com.example.android.ui.base.BaseFragment;
import com.example.android.utils.CircleImageView;

import java.util.List;

public class MineFragment extends BaseFragment implements MineContract.IMineView, View.OnClickListener {
    private MineContract.IMinePresenter minePresenter;
    private ImageView imgMineRelease, imgMineSell, imgMineBuy, imgMineResell,imgMineSet;
    private CircleImageView headImg;
    private TextView account,name,favoriteNum,historyNum,focusNum,fansNum;
    private static final String TAG = "MineFragment";
    @Override
    protected void initViews() {
        favoriteNum =find(R.id.mine_text_favorite_num);
        historyNum =find(R.id.mine_text_history_num);
        focusNum =find(R.id.mine_text_focus_num);
        fansNum =find(R.id.mine_text_fans_num);
        headImg = find(R.id.mine_circle_image);
        account = find(R.id.mine_text_account);
        name = find(R.id.mine_text_name);
        minePresenter = new MinePresenter(this);
        imgMineRelease = find(R.id.imgMineRelease);
        imgMineSell = find(R.id.imgMineSell);
        imgMineBuy = find(R.id.imgMineBuy);
        imgMineResell = find(R.id.imgMineResell);
        imgMineSet = find(R.id.imgMineSet);
        //加载数据
        initLoadData();
        minePresenter.getData();
        //点击事件
        imgMineSet.setOnClickListener(this);

    }
    public void initLoadData(){
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("HBUserLogin",0);
        String userUsername = sharedPreferences.getString("userUsername","");
        String userAccount = sharedPreferences.getString("userAccount","");
        String userHeadimageurl = sharedPreferences.getString("userHeadimageurl","");
//        Log.i("MineFragment",userUsername+ " "+ userAccount+" "+userHeadimageurl)
        name.setText(userUsername);
        account.setText("账号："+userAccount);
        favoriteNum.setText("0");historyNum.setText("0");
        focusNum.setText("0");fansNum.setText("0");

        Glide.with(this).load(userHeadimageurl).into(headImg);
    }

    @Override
    public void onResume() {
        super.onResume();
        initLoadData();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void getMineSuccess(BaseBean<Mine> mine) {
//        Log.i(TAG, mine.getData().toString());
        setMineIcon(imgMineRelease, mine.getData().getRelease());
        setMineIcon(imgMineBuy,mine.getData().getBuy());
        setMineIcon(imgMineSell,mine.getData().getSell());
        setMineIcon(imgMineResell,mine.getData().getResell());
    }

    @Override
    public void getMineError(Throwable throwable) {

    }

    public void setMineIcon(ImageView imageView, String url) {
        Glide.with(this)
                .load(url)
                .into(imageView);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.imgMineSet:
                Intent intent = new Intent(getActivity(),SetActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
